package cn.musijas.service.api;

import cn.musi.constant.RedisKeysConstant;
import cn.musi.product.api.IProductService;
import cn.musijas.service.entity.Product;
import cn.musijas.service.mapping.ProductMapping;
import cn.musijas.service.mapping.ProductRecordMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductRecordMapping  productRecordService;
    @Autowired
    private ProductMapping productService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate ;
    @Override
    public Integer getCurrentCount() throws Exception {

        String count = redisTemplate.opsForValue().get(RedisKeysConstant.STOCK_COUNT + 1);
        if (count == null) {
            Product stock = productService.selectByPrimaryKey(1);
            count = stock.getCount().toString() ;
            redisTemplate.opsForValue().set(RedisKeysConstant.STOCK_COUNT + 1, stock.getCount().toString());
            redisTemplate.opsForValue().set(RedisKeysConstant.STOCK_SALE + 1, stock.getSale().toString());
            redisTemplate.opsForValue().set(RedisKeysConstant.STOCK_VERSION + 1, stock.getVersion().toString());
        }
        return Integer.parseInt(count);
    }
}
